﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestSharp;

namespace Esign.Net.Input
{
    [Serializable]
    public partial class SignFlow
    {
        #region 基于文件发起签署 实体

        /// <summary>
        /// 基于文件发起签署 实体
        /// </summary>
        public class CreateByFile : InputBase, IInput
        {
            [JsonProperty("attachments")]
            public IList<Attachment> Attachments { get; set; }

            [JsonProperty("docs")]
            public IList<Doc> Docs { get; set; }

            /// <summary>
            /// 签署方信息（指参与签署的个人或者机构）
            ///单个签署方数组中，机构签署方、个人签署方二选一传入
            ///（orgSignerInfo与psnSignerInfo二选一即可）；
            ///
            ///多方签署场景，可传多个签署方数组；
            ///单个签署流程中，签署方最多不能超过300个。
            /// </summary>
            [JsonProperty("signers")]
            public IList<Signer> Signers { get; set; }

            /// <summary>
            /// 签署流程配置项
            /// </summary>
            [JsonProperty("signFlowConfig")]
            public SignFlowConfig SignFlowConfig { get; set; }

            [JsonProperty("signFlowInitiator")]
            public SignFlowInitiator SignFlowInitiator { get; set; }


            public T Build<T>(T t, Config config) where T : InputBase, new()
            {
                //多方签署 ，自动盖章 https://open.esign.cn/doc/opendoc/case3/evesulzd0efhg59y#GtE8a

                t.ConfigInfo = config;
                //https://open.esign.cn/doc/opendoc/pdf-sign3/su5g42
                t.Url = "/v3/sign-flow/create-by-file";
                t.ReqType = Method.POST;
                return t;
            }
        }

        #endregion

        #region 开启流程

        [Serializable]
        public class Start : InputBase, IInput
        {
            /// <summary>
            /// 流程id
            /// </summary>
            [JsonIgnore]
            public string SignFlowId { get; set; }

            public T Build<T>(T t, Config config) where T : InputBase, new()
            {
                t.ConfigInfo = config;
                //https://open.esign.cn/doc/opendoc/pdf-sign3/pu4xsx
                t.Url = $"/v3/sign-flow/{this.SignFlowId}/start";
                t.ReqType = Method.POST;
                return t;
            }
        }

        #endregion

        #region 获取签署页面链接 实体

        public class SignUrl : InputBase, IInput
        {
            /// <summary>
            /// 流程id
            /// </summary>
            [JsonIgnore]
            public string SignFlowId { get; set; }

            /// <summary>
            /// 链接类型（默认值 2）
            /// 1 - 预览链接（仅限查看，不能签署）， 2 - 签署链接
            /// </summary>
            [JsonProperty("urlType")]
            public int UrlType { get; set; } = 2;

            /// <summary>
            /// 指定客户端类型，当urlType为2（签署链接）时生效
            ///H5 - 移动端适配
            ///PC - PC端适配
            ///ALL - 自动适配移动端或PC端（默认值）
            ///【注】参数值均为大写的英文
            /// </summary>
            [JsonProperty("clientType")]
            public string ClientType { get; set; } = "ALL";

            [JsonProperty("operator")]
            public Operator Operator { get; set; }

            /// <summary>
            /// 重定向配置项
            /// </summary>
            [JsonProperty("redirectConfig")]
            public RedirectConfig RedirectConfig { get; set; }

            public T Build<T>(T t, Config config) where T : InputBase, new()
            {
                t.ConfigInfo = config;
                //https://open.esign.cn/doc/opendoc/pdf-sign3/pvfkwd
                t.Url = $"/v3/sign-flow/{this.SignFlowId}/sign-url";
                t.ReqType = Method.POST;
                return t;
            }
        }
        #endregion

        #region 完成流程 

        [Serializable]
        public class Finish : InputBase, IInput
        {
            /// <summary>
            /// 流程id
            /// </summary>
            [JsonIgnore]
            public string SignFlowId { get; set; }

            public T Build<T>(T t, Config config) where T : InputBase, new()
            {
                t.ConfigInfo = config;
                //https://open.esign.cn/doc/opendoc/pdf-sign3/ynwqsm
                t.Url = $"/v3/sign-flow/{SignFlowId}/finish";
                t.ReqType = Method.POST;
                return t;
            }
        }

        #endregion

        #region 撤销签署流程 

        [Serializable]
        public class Revoke : InputBase, IInput
        {
            /// <summary>
            /// 流程id
            /// </summary>
            [JsonIgnore]
            public string SignFlowId { get; set; }

            /// <summary>
            /// 撤销原因 非必填【注】撤销原因最多50字
            /// </summary>
            [JsonProperty("revokeReason")]
            public string RevokeReason { get; set; }


            public T Build<T>(T t, Config config) where T : InputBase, new()
            {
                t.ConfigInfo = config;
                //https://open.esign.cn/doc/opendoc/pdf-sign3/klbicu
                t.Url = $"/v3/sign-flow/{SignFlowId}/revoke";
                t.ReqType = Method.POST;
                return t;
            }
        }

        #endregion

        #region 查询流程 

        [Serializable]
        public class Detail : InputBase, IInput
        {
            /// <summary>
            /// 流程id
            /// </summary>
            [JsonIgnore]
            public string SignFlowId { get; set; }

            public T Build<T>(T t, Config config) where T : InputBase, new()
            {
                t.ConfigInfo = config;
                //https://open.esign.cn/doc/opendoc/pdf-sign3/xxk4q6
                t.Url = $"/v3/sign-flow/{SignFlowId}/detail";
                t.ReqType = Method.GET;
                return t;
            }
        }

        #endregion

    }
}
